from copy import deepcopy

from app.settings import BASE_DIR
from utils import write_file, read_from_file, DELIMITER, clean_path

docker_service_module = read_from_file("{}/deploy/module/docker/docker.service".format(BASE_DIR))

DOCKER_SERVER_NAME = "docker.service"


def gen_docker_service(out_dir, work_dir, insecure_registry_list, dns_list):
    docker_service = deepcopy(docker_service_module)

    insecure_registry = ["--insecure-registry={}".format(each) for each in insecure_registry_list]
    insecure_registry_args = DELIMITER.join(insecure_registry)
    dns = ["--dns={}".format(each) for each in dns_list]
    dns_args = DELIMITER.join(dns)

    docker_service = docker_service.format(
        data_dir=clean_path(work_dir + "/data"),
        exec_dir=clean_path(work_dir + "/exec"),
        insecure_registry_args=insecure_registry_args,
        dns_args=dns_args
    )

    path = clean_path(out_dir + "/" + DOCKER_SERVER_NAME)
    write_file(path, docker_service)
    return path
